package edu.hunau.cxb19.oop.class24;

import java.util.Arrays;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

/**
 *
 *@author hb
 *
 * 2020-03-27 14:53:53
 */
public class MapTest {

	@Test
	public void mapTest() {
		List<Integer> list = Arrays.asList(1,2,3,4,5);
		//获得一个新的序列(2,4,9,16,25)
		List<Integer> result = list.stream()
								    .map(n->n*n)
								    .collect(Collectors.toList());
		System.out.println(result);
	}
	/**
	 * 两个数字列表，获得所有数字对
	 * [1,2,3] 和 [5,6]
	 * 结果
	 * [(1,5),(1,6),(2,5),(2,6),(3,5),(3,6)] ->List< int[] >
	 */
	@Test
	public void mapTest02() {
		List<Integer> list01 = Arrays.asList(1,2,3);
		List<Integer> list02 = Arrays.asList(5, 6);
		List<Stream<int[]>> result01 = list01.stream()
		      .map(i-> list02.stream().map(j->new int[] {i,j}))
		      .collect(Collectors.toList());
		
		  List<int[]> result = list01.stream()
			      .flatMap(i-> list02.stream().map(j->new int[] {i,j}))
			      .collect(Collectors.toList());
		  result.forEach(s->System.out.println(Arrays.toString(s)));
	}
}
